package org.springcloud.wisdom.spring_02_classroom.admin.createcourse.controller;

import org.springcloud.wisdom.spring_02_classroom.admin.createcourse.entity.Course;
import org.springcloud.wisdom.spring_02_classroom.admin.createcourse.service.CourseService;
import org.springcloud.wisdom.spring_02_classroom.bean.BaseController;
import org.springcloud.wisdom.spring_02_classroom.bean.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/course")
public class CourseController extends BaseController {
    @Autowired
    private CourseService courseService;

    //开启选课
    @PutMapping("/canlook")
    public ResponseData<Object> updatecan(@RequestBody Course course) {
        try {
            boolean success = courseService.updatecan(course);
            if (!success) {
                return new ResponseData<>(400, "您已开启选课！", null);
            }
            return new ResponseData<>(200, "开启选课！", null);
        } catch (Exception e) {
            return new ResponseData<>(500, "网络异常", null);
        }
    }

    //关闭选课
    @PutMapping("/cantlook")
    public ResponseData<Object> updatecant(@RequestBody Course course) {
        try {
            boolean success = courseService.updatecant(course);
            if (!success) {
                return new ResponseData<>(400, "您已关闭选课！", null);
            }
            return new ResponseData<>(200, "关闭选课！", null);
        } catch (Exception e) {
            return new ResponseData<>(500, "网络异常", null);
        }
    }

    // 创建课程
    @PostMapping("/add")
    public ResponseData<Object> addCourse(@RequestBody Course course) {
        try {
            boolean success = courseService.addCourse(course);
            if (!success) {
                return new ResponseData<>(400, "添加失败，所选时间和地点的教室已被占用或教师时间冲突", null);
            }
            return new ResponseData<>(200, "添加成功！", null);
        } catch (Exception e) {
            return new ResponseData<>(500, "请检查您的输入内容是否重复", null);
        }
    }

    // 更新课程信息
    @PutMapping("/update")
    public ResponseData<Object> updateCourse(@RequestBody Course course) {
        try {
            boolean success = courseService.updateCourse(course);
            if (!success) {
                return new ResponseData<>(400, "修改失败，请检查您的内容是否冲突或非法", null);
            }
            return new ResponseData<>(200, "修改成功！", null);
        } catch (Exception e) {
            return new ResponseData<>(500, "请检查您的输入内容是否重复", null);
        }
    }

    // 删除课程
    @GetMapping("/delete/{cid}")
    public ResponseData<Object> deleteCourse(@PathVariable String cid) {
            boolean success = courseService.deleteCourse(cid);
            if (success) {
                return returnObject(true);
            } else {
                return returnObject(false);
            }
    }

    //分页条件查课程
    @RequestMapping("/all")
    public ResponseData<Object> getPage(@RequestParam("pageNum") int pageNum,
                                        @RequestParam("pageSize") int pageSize,
                                        @RequestParam(value = "cid", required = false) String cid,
                                        @RequestParam(value = "tid", required = false) String tid) {
        return returnObject(courseService.AllCourse(pageNum, pageSize, cid,tid));
    }

    //分页条件查课程
    @RequestMapping("/allH")
    public ResponseData<Object> getPageH(@RequestParam("pageNum") int pageNum,
                                        @RequestParam("pageSize") int pageSize,
                                        @RequestParam(value = "cid", required = false) String cid,
                                        @RequestParam(value = "tid", required = false) String tid) {
        return returnObject(courseService.AllCourseH(pageNum, pageSize, cid,tid));
    }
}